/**
 * Created by mtagent on 1/20/16.
 */

index_app.factory('alertService', function($rootScope) {

    var alertService ={};

    // 创建一个全局的 alert 数组
    $rootScope.alerts = [];

    alertService.add = function(type, msg) {
        $rootScope.alerts.push(
            {
                'type': type,
                'msg': msg,
                'close': function(){
                    alertService.closeAlert(this);
                }
            });
    };

    alertService.closeAlert = function(alert) {
        alertService.closeAlertIdx($rootScope.alerts.indexOf(alert));
    };

    alertService.closeAlertIdx = function(index) {
        $rootScope.alerts.splice(index, 1);
    };
    return alertService;
});

index_app.factory('httpInterceptor', ['$q', '$rootScope', '$log',  function($q, $rootScope, $log) {

    var numLoadings = 0;

    var httpInterceptor = {

        request: function (config) {

            numLoadings++;

            // Show loader
            $rootScope.$broadcast("loader_show");
            return config || $q.when(config)

        },
        response: function (response) {

            if ((--numLoadings) === 0) {
                // Hide loader
                $rootScope.$broadcast("loader_hide");
            }

            return response || $q.when(response);

        },
        responseError: function (response) {

            if (!(--numLoadings)) {
                // Hide loader
                $rootScope.$broadcast("loader_hide");
            }

            return $q.reject(response);
        }
    };

    return httpInterceptor;
}]);

index_app.factory('dataFactory', ['$http', function($http) {

    var dataFactory = {};
    var url_base = protocol + host + ':' + port;
    var header = {'Content-Type': 'application/x-www-form-urlencoded'};

    dataFactory.get_envs = function(start, page_size) {
        var api = "/v1/environments?start=" + start + "&page_size=" + page_size;
        return $http.get(url_base + api);
    }

    dataFactory.get_env_by_id = function(env_id) {
        var api = "/v1/environment/" + env_id;
        return $http({
            method : 'get',
            url : url_base + api
        });
    }

    dataFactory.get_env_by_name = function(name) {
        var api = "/v1/environment/" + name;
        return $http({
            method : 'get',
            url : url_base + api
        });
    }

    dataFactory.delete_env = function(env) {
        var api = "/v1/environment/delete";

        return $http({
            method: 'post',
            url: url_base + api,
            data: env,
            headers : header
        });
    }

    dataFactory.update_env = function(env) {
        var api = "/v1/environment/update";
        return $http({
                method: 'post',
                url: url_base + api,
                data: env,
                headers: header
            });
    }

    dataFactory.create_env = function(post_data) {
        var api = "/v1/environment/create";
        return $http({
            method: 'post',
            url: url_base + api,
            data: post_data,
            headers: header
        });
    }

    dataFactory.get_scopelines_by_envname = function(env_name, start, size) {

        var api = "/v1/scopelines/list_by_env?env_name=" + env_name + "&start=" + start + "&page_size=" + size;

        return $http({
            method : 'get',
            url : url_base + api
        });
    }

    // env =  { EnvironmentID: 1, Name : ''}
    dataFactory.init_scopeline_by_env_id = function(env) {

        var api = "/v1/scopeline/init";

        return $http({
            method : 'post',
            url : url_base + api,
            data : env,
            headers :header
        });
    }

    return dataFactory;

}]);


